package com.ymatou.shop.ui.activity;

import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.LoginResult;
import com.ymatou.shop.model.ResponseMessage;
import com.ymatou.shop.ui.activity.buyer.OrderActivity;
import com.ymatou.shop.ui.activity.seller.AddProductActivity;
import com.ymatou.shop.ui.activity.seller.CameraActivity;
import com.ymatou.shop.ui.activity.seller.MessageActivity;
import com.ymatou.shop.util.DialogUtil;
import com.ymatou.shop.util.GlobalUtil;
import com.ymatou.shop.util.LogUtil;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UserinfoActivity extends BaseHeaderActivity implements OnClickListener{

	private View orderManagerView, messageCenterView, publishedView, logoutView;
	private TextView orderCount, messageCount, userNickTextView, leftCashTextView, leftCashTextView2;
	private ImageView headImageView;
	private static final int REQUEST_ID_GET_NEW_MESSAGE = 0x00;
	private static final int REQUEST_ID_GET_NEW_ORDER = 0x01;
	private static final int REQUEST_ID_CANCEL_PERMISSION = 0x02;
	private static final int REQUEST_ID_ACCOUNT_INFO = 0x03;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userinfo_activity);

		setupViews();

	}

	@Override
	protected void setupViews() {
		super.setupViews();

		leftCashTextView = (TextView)findViewById(R.id.content_textview);
		leftCashTextView2 = (TextView)findViewById(R.id.leftcash_textview);
		
		headImageView = (ImageView)findViewById(R.id.user_head_imageview);
		userNickTextView = (TextView)findViewById(R.id.user_name_textview);
		
		
		Drawable drawable = ImageCache.get(this, getAccount().getIcon(), new ImageCache.OnIconLoadCompleteListener() {
			@Override
			public void onLoadComplete(String url, boolean success, Drawable drawable) {
				if (success) {
					headImageView.setImageDrawable(drawable);
				}
			}
		});
		if (drawable != null) {
			headImageView.setImageDrawable(drawable);
		}
		
		userNickTextView.setText(getAccount().getNickName());
		
		findViewById(R.id.logout_textview).setOnClickListener(this);
		
		
		if (getAccount().isSeller()){
			findViewById(R.id.new_product).setVisibility(View.VISIBLE);
			findViewById(R.id.new_product).setOnClickListener(this);
			
			publishedView = findViewById(R.id.published_product);
			publishedView.setVisibility(View.VISIBLE);
			publishedView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					//go to 已发布的商品

				}
			});
			((TextView) publishedView.findViewById(R.id.textview)).setText(getString(R.string.published_product_label));
		}
		

		
		orderManagerView = findViewById(R.id.oder_manage);
		orderManagerView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//go to 订单管理
				if (getAccount().isSeller()){
					GlobalUtil.startActivity(v.getContext(), OrderActivity.class);	
				}else {
					GlobalUtil.startActivity(v.getContext(), com.ymatou.shop.ui.activity.seller.OrderActivity.class);
				}
				
			}
		});
		((TextView) orderManagerView.findViewById(R.id.textview)).setText(getString(R.string.my_order_label));
		orderCount = (TextView) orderManagerView.findViewById(R.id.textview_count);

		messageCenterView = findViewById(R.id.message_center);
		messageCenterView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//go to 消息中心
				GlobalUtil.startActivity(v.getContext(), MessageActivity.class);
			}
		});
		((TextView) messageCenterView.findViewById(R.id.textview)).setText(getString(R.string.message_center_label));
		messageCount = (TextView) messageCenterView.findViewById(R.id.textview_count);
		
		RequestInfo request = ApiRequest.getNewCommentNum();
		getCacheManager().register(REQUEST_ID_GET_NEW_MESSAGE, request, this);

		RequestInfo request2 = ApiRequest.getNewOrderNum();
		getCacheManager().register(REQUEST_ID_GET_NEW_ORDER, request2, this);
		
		if (!getAccount().isSeller()){
			RequestInfo request3 = ApiRequest.getAccountInfo();
			getCacheManager().register(REQUEST_ID_ACCOUNT_INFO, request3, this);	
		}
		
	}
	
	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo, Object response) {
		LogUtil.systemErr((String) response);
		if (requestId == REQUEST_ID_GET_NEW_MESSAGE){
			
			int mNewCommentNum = ApiResponse.getNewCommentNum((String) response);
			if (mNewCommentNum > 0){
				messageCount.setText(""+mNewCommentNum);
				messageCount.setVisibility(View.VISIBLE);
			}
			
		}else if (requestId == REQUEST_ID_GET_NEW_ORDER) {
			int mNewOrderNum = ApiResponse.getNewOrderNum((String) response);
			if (mNewOrderNum > 0){
				orderCount.setText(""+mNewOrderNum);
				orderCount.setVisibility(View.VISIBLE);
			}
			
		}else if (requestId ==  REQUEST_ID_CANCEL_PERMISSION) {
			ResponseMessage mResponseMessage = ApiResponse.canclePermission((String) response);
			if (mResponseMessage.status == 200){
				DialogUtil.toast(this, R.string.logout_success);
				GlobalUtil.startActivity(this, HomeActivity.class);
				cancelProgressDialog();
				finish();
			}
		}else if (requestId == REQUEST_ID_ACCOUNT_INFO) {
			LoginResult mLoginResult = ApiResponse.getAccountInfo((String) response);
			leftCashTextView2.setText(mLoginResult.mAvailAmount);
			leftCashTextView2.setVisibility(View.VISIBLE);
			leftCashTextView.setVisibility(View.VISIBLE);
		}
			super.onCacheSuccess(requestId, requestInfo, response);	
	}
	
	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo, CacheException error) {
	 if (requestId ==  REQUEST_ID_CANCEL_PERMISSION) {
		 
	 }
	  super.onCacheFailed(requestId, requestInfo, error);
	}

	@Override
  public void onClick(View v) {
		switch (v.getId()) {
		case R.id.logout_textview:
			new AlertDialog.Builder(this).setMessage(R.string.logout_sure).setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			}).setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					setProgressDialogMessage(R.string.logout_ing_text);
					showProgressDialog();
					cancelPermission();
					getAccount().logout();
					dialog.cancel();
				}
			}).show();
			break;
		case R.id.new_product:
			GlobalUtil.startActivity(this, CameraActivity.class);
			break;
		default:
			break;
		}
	  
  }
	
	private void cancelPermission() {
		RequestInfo requestInfo = ApiRequest.cancelPermission();
		getCacheManager().register(REQUEST_ID_CANCEL_PERMISSION, requestInfo, this);
	}
}
